package com.cavedroid;

import javax.microedition.khronos.opengles.GL10;

public class DualObstacles extends Obstacles {

	public DualObstacles(CubeDrawer cubeDrawer) {
		super(cubeDrawer);
		DIMS = 6;
		array = new float[MAX_NUMBER_OF_OBSTACLES*DIMS];
	}
	
	@Override
	public void drawShape(GL10 gl) {
		float x, y, z, offset, width, angle;
		int arrayIndex = 0;
		for (int i = 0; i < count; i++) {
			x = array[arrayIndex++];
			y = array[arrayIndex++];
			z = array[arrayIndex++];
			offset = array[arrayIndex++];
			width = array[arrayIndex++];
			angle = array[arrayIndex++];
			gl.glColor4f(1,0,1,0.5f);
			drawRect(gl, x, y, z, offset, width, angle);
		}
	}
	
	public void drawRect(GL10 gl, float x, float y, float z, float offset, float width, float angle) {
		gl.glPushMatrix();
		gl.glTranslatef(x, y, z);
		gl.glRotatef(angle, 0, 0, 1);
		gl.glTranslatef(0, offset, 0);
		gl.glScalef(width, HEIGHT, DEPTH);
		cube.draw(gl);
		gl.glPopMatrix();
	}
	
	@Override
	protected float getDistanceFromIndex(int index, float x, float y, float z) {
		if (array[index*DIMS+5] == 90) {
			return Geometry.distance2(x*0.8f, z, (array[index*DIMS]-array[index*DIMS+3])*0.8f, array[index*DIMS+2]);
		} else {
			return Geometry.distance2(y, z, array[index*DIMS+1]+array[index*DIMS+3], array[index*DIMS+2]);
		}
	}
	
	@Override
	public void newObstacle(float x, float y, float z, float limit) {
		int index = point*DIMS;
		float offset = limit*((float)Math.random()*2-1)*0.5f;

		array[index++] = x;
		array[index++] = y;
		array[index++] = z;
		array[index++] = offset;
		array[index++] = (float)Math.sqrt(Math.pow(limit,2) - offset*offset)*2*0.8f;
		array[index++] = (Math.random() > 0.5) ? 0 : 90;
		if (count < MAX_NUMBER_OF_OBSTACLES) {
			count++;
		}
		point++;
		point %= MAX_NUMBER_OF_OBSTACLES;
	}

}
